<template>
  <div>
    <el-table
        :data="tableData"
        @expand-change="change"
        style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="角色id">
              <span>{{ props.row.rid }}</span>
            </el-form-item>
            <el-form-item label="菜单">
              <span>{{rowData[props.row.rid]}}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column
          label="角色ID"
          prop="rid">
      </el-table-column>
      <el-table-column
          label="角色名"
          prop="name">
      </el-table-column>
      <el-table-column
          label="中文名"
          prop="note">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {onMounted, ref} from "vue";
import {getRoleById, getRoles} from "../../api/role";

export default {
  setup() {
    const tableData = ref()
    const rowData = ref([])
    // 编辑按钮
    const handleEdit = (index, row) => {
      console.log(row);
    }
    // 删除按钮
    const handleDelete = (index, row) => {
      console.log(index, row);
    }
    const change = (row, old) => {
      if (old.indexOf(row) !==-1){
       getRoleById(row.rid).then(res=>{
          rowData.value[row.rid] = res.data
       }).catch(e=>{console.log(e)})
      }else {
        rowData.value[row.rid]=null
      }
    }
    onMounted(()=>{
      getRoles().then(res=>{
        tableData.value = res.data
      }).catch()
    })
    return{
      tableData,
      handleEdit,
      handleDelete,
      change,
      rowData
    }
  }
}
</script>

<style scope>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
